<template>
  <div class="mdlxgl">
    <a-search :change="paginationElement?.change" :list="list" v-model="form"></a-search>
    <a-card resize>
      <a-card>
        <el-button @click="handleInfo(null, true)">新增</el-button>
      </a-card>
      <a-table :data="data" :head="head">
        <template #handle="{ row }">
          <el-link @click="handleInfo(row, true)">编辑</el-link>
          <el-link type="primary" @click="handleInfo(row, false)">查看</el-link>
          <a-link type="danger" :confirm="() => handleDelete(row)">删除</a-link>
        </template>
      </a-table>
      <a-pagination ref="paginationElement" :total="total" :change="getTableData"></a-pagination>
      <a-dialog :title="title" v-model="show" :footer="edit" :confirm="handleConfirm">
        <a-form ref="formElement" :list="rowList" v-model="rowForm" :inline="true" :disabled="!edit"></a-form>
      </a-dialog>
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { tableData, save, deleteData } from '../../api/mdlxgl/mdxlgl'
import deptTree from '../../store/deptTree'
import { ElMessage } from 'element-plus'

interface MDLXGL {
  id: string
  leadDept: number[]
  leadDeptObj: DeptTree[]
  matterType: string
  leadDeptLabel: string
  [key: string]: unknown
}

const paginationElement = ref<APaginationInstance | null>()
const formElement = ref<AFormInstance | null>(null)

const type = [
  { label: '红名单', value: '1' },
  { label: '黑名单', value: '0' }
]

const form = ref({})
const list = ref<FormItem[]>([
  { prop: 'matterName', 'aria-label': '名单类型名称', type: 'input' },
  { prop: 'matterType', 'aria-label': '名单类型', type: 'select', data: type }
])

const data = ref<MDLXGL[]>([])
const head = ref([
  { prop: 'matterName', label: '名单名称' },
  { prop: 'matterTypeLabel', label: '名单类型' },
  {
    prop: 'leadDeptLabel',
    label: '牵头部门'
  },
  { prop: 'handle', label: '操作', fixed: 'right' }
])
const total = ref(0)

function getTableData(row: number, size: number) {
  tableData<MDLXGL>({
    size,
    row,
    ...form.value
  }).then(({ data: result }) => {
    data.value = result.pageList.map((item) => {
      return {
        ...item,
        leadDeptLabel: item.leadDept.map((i) => deptTree.getDeptTreeNameById(i)).join(','),
        matterTypeLabel: type.find((i) => i.value === item.matterType)?.label
      }
    })
    total.value = result.totals
  })
}

const rowForm = ref<MDLXGL>({} as MDLXGL)
const rowList = ref<FormItem[]>([
  { prop: 'matterName', 'aria-label': '名单名称', type: 'input', rules: [{ required: true, message: '请输入名单名称', trigger: 'blur' }] },
  { prop: 'matterType', 'aria-label': '名单类型', type: 'select', data: type, rules: [{ required: true, message: '请选择名单类型', trigger: 'change' }] },
  {
    prop: 'leadDeptObj',
    'aria-label': '牵头部门',
    type: 'deptTree2',
    rules: [{ required: true, message: '请选择牵头部门', trigger: 'change' }],
    formItemProps: {
      style: {
        width: '100%'
      }
    }
  }
])

const show = ref(false)
const title = ref('详情')
const edit = ref(false)

function handleInfo(row: MDLXGL | null, e: boolean) {
  rowForm.value = { ...row } as MDLXGL

  rowForm.value.leadDeptObj = row
    ? rowForm.value.leadDept.map((i) => {
        return {
          id: i,
          label: deptTree.getDeptTreeNameById(i)
        }
      })
    : []
  edit.value = e
  show.value = true
  title.value = e ? '编辑' : '查看'
}

function handleConfirm() {
  formElement.value?.element.validate((valid: boolean) => {
    if (valid) {
      save({
        ...rowForm.value,
        leadDept: rowForm.value.leadDeptObj.map((item) => item.id)
      }).then(({ msg }) => {
        show.value = false
        paginationElement.value?.change()
        ElMessage.success(msg)
      })
    }
  })
}

function handleDelete(row: MDLXGL) {
  deleteData(row.id).then(({ msg }) => {
    ElMessage.success(msg)
    paginationElement.value?.change()
  })
}
</script>
